package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

import java.awt.image.*;
import java.awt.geom.*;
import java.awt.Robot;

/**  <pre>
     <b>类功能描述：</b><pre>
     完成 登录组合框 功能要求。
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonLoginCombox extends JPanel {

    int comboxWidth;
    int comboxHeight;

    int clipBgStartX0;
    int clipBgStartY0;
    int slideBgStartX0;
    int slideBgStartY0;
    int slideButtonX;
    int slideButtonY;
    int slideButtonMinY;
    int slideButtonMaxY;

    int comboxTextSelectedNumber;
    int comboxTextStartX;
    int comboxTextStartY;
    int comboxTextStartMaxY;
    int comboxTextStartMinY;
    int comboxTextLineHeight;
    int comboxChoiceIndex;

    int comboxTextDeleteButtonStartX;

    BufferedImage unSelectedImage;
    BufferedImage selectedImage;
    BufferedImage slideBackgroundImage;

    java.util.ArrayList comboxVisibleNameList;
    java.util.ArrayList comboxVisibleSexList;
    java.util.ArrayList comboxVisibleBirthList;

    java.util.ArrayList comboxAllNameList;
    java.util.ArrayList comboxAllSexList;
    java.util.ArrayList comboxAllBirthList;

    String filtStr="";

    boolean isComboxHide=true;

    JTextField birthYearField;
    JTextField birthMonthField;
    JTextField birthDayField;
    JTextField userNameField;
    JButton manButton;
    JButton womanButton;

    JPanel userNamePanel;

    JDialog deleteWindow;
    int dialogX0=282;
    int dialogY0=275;
    int dialogWidth=459;
    int dialogHeight=217;

    String deletingUser;

    public void setUserNamePanel(JPanel userNamePanel){
        this.userNamePanel=userNamePanel;
    } 
    public JPanel getUserNamePanel(){
        return userNamePanel;
    }

    public int getComboxAllNameListSize(){
        return comboxAllNameList.size();
    }

    public String getComboxAllNameListElement(int i){
        return (String)comboxAllNameList.get(i);
    }

    public String getComboxAllBirthListElement(int i){
        return (String)comboxAllBirthList.get(i);
    }

    public String getComboxAllSexListElement(int i){
        return (String)comboxAllSexList.get(i);
    }

    public void setFiltString(String filtStr){
        this.filtStr=filtStr;
        comboxVisibleNameList  = new java.util.ArrayList();
        comboxVisibleSexList   = new java.util.ArrayList();
        comboxVisibleBirthList = new java.util.ArrayList();
        if (filtStr==null)
            filtStr="";
        filtStr=filtStr.toLowerCase().trim();
        String str;
        for (int i=0; i<comboxAllNameList.size(); i++) {
            str=(String)comboxAllNameList.get(i);
            if (str.toLowerCase().startsWith(filtStr)) {
                comboxVisibleNameList.add(str);
                str=(String)comboxAllSexList.get(i);
                comboxVisibleSexList.add(str);
                str=(String)comboxAllBirthList.get(i);
                comboxVisibleBirthList.add(str);
            }
        }

        if (comboxVisibleNameList.size()<1)
            isComboxHide=true;
    }

    public void setWomanButton(JButton womanButton){
        this.womanButton=womanButton;
    } 
    public JButton getWomanButton(){
        return womanButton;
    }
 
    public void setManButton(JButton manButton){
        this.manButton=manButton;
    } 
    public JButton getManButton(){
        return manButton;
    }
 
    public void setUserNameField(JTextField userNameField){
        this.userNameField=userNameField;
    } 
    public JTextField getUserNameField(){
        return userNameField;
    }

    public void setBirthYearField(JTextField birthYearField){
        this.birthYearField=birthYearField;
    } 
    public JTextField getBirthYearField(){
        return birthYearField;
    }

    public void setBirthMonthField(JTextField birthMonthField){
        this.birthMonthField=birthMonthField;
    } 
    public JTextField getBirthMonthField(){
        return birthMonthField;
    }
    public void setBirthDayField(JTextField birthDayField){
        this.birthDayField=birthDayField;
    } 
    public JTextField getBirthDayField(){
        return birthDayField;
    }
 
    public void setComboxHide(boolean isComboxHide){
        this.isComboxHide=isComboxHide;
    }

    public boolean isComboxHide(){
        return this.isComboxHide;
    }

    public ElfiiCommonLoginCombox(int comboxWidth,int comboxHeight) { //-入口参数必须把宽度和高度送进来，其它值要据此初始化

        this.comboxWidth=comboxWidth;
        this.comboxHeight=comboxHeight;

        File f=null;
        comboxVisibleNameList  = new java.util.ArrayList();
        comboxVisibleSexList   = new java.util.ArrayList();
        comboxVisibleBirthList = new java.util.ArrayList();

        try {
            f=new File("resource-common/images/gw/gw_login_slide_bg.jpg");
            slideBackgroundImage=ImageIO.read(f);
            f=new File("resource-common/images/login/combox_not_selected.png");
            unSelectedImage=ImageIO.read(f);
            f=new File("resource-common/images/login/combox_selected.png");
            selectedImage=ImageIO.read(f);
        } catch(Exception e) {System.out.print(e+"   "+f);}

        slideBgStartX0=comboxWidth-45;
        slideBgStartY0=0;
        slideButtonMinY=slideBgStartY0+60;
        slideButtonMaxY=slideBgStartY0+73;
        slideButtonX=slideBgStartX0+11;
        slideButtonY=slideButtonMinY;

        clipBgStartX0=5;
        clipBgStartY0=5;

        comboxTextSelectedNumber=0;

        comboxTextLineHeight=50;
        comboxTextStartMaxY=clipBgStartY0+5;

        //-textStrings.length-9,其中的9是clip已经显示的内容，余下的内容才是需要向上移的。
        //-+20只是为了多留出些空间而已。
        comboxTextStartX=clipBgStartX0;
        comboxTextStartY=comboxTextStartMaxY; 

        comboxTextDeleteButtonStartX=slideBgStartX0-30;
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g ;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
        g2.setColor(Color.black);
        g2.fillRect(0,0,comboxWidth,comboxHeight);
        String str;

        if (isComboxHide==false){

            comboxTextStartMinY=0-(comboxVisibleNameList.size()-2)*comboxTextLineHeight+comboxTextStartMaxY; 
            g2.setColor(Color.lightGray);
            g2.fillRect(0,0,comboxWidth,comboxHeight/2);
            g2.fillRoundRect(0,2,comboxWidth,comboxHeight-2,50,50);

            g2.setStroke(new BasicStroke(8.0f));

            g2.drawImage(slideBackgroundImage,null,slideBgStartX0,slideBgStartY0);

            g2.setStroke(new BasicStroke(7.0f));
            g2.setColor(Color.white);

            g2.fillOval(slideButtonX,slideButtonY-20,20,20);
            g2.setClip(clipBgStartX0,clipBgStartY0,comboxWidth-10,comboxHeight-13);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,26));
            for (int i=0; i<comboxVisibleNameList.size(); i++) {
                g2.setColor(Color.darkGray);
                if (comboxChoiceIndex==i)
                    g2.setColor(ElfiiCommonConstant.Elfii_Blue);
                g2.fillRoundRect(comboxTextStartX,comboxTextStartY+comboxTextLineHeight*i,comboxWidth-50,comboxTextLineHeight-6,35,35);

                str=(String)comboxVisibleNameList.get(i);

                g2.setColor(Color.white);
                g2.drawString(str,comboxTextStartX+15,comboxTextStartY+comboxTextLineHeight*i+30);  //+30是因为字体是从下向上画的
                g2.drawString("x",comboxWidth-70,comboxTextStartY+comboxTextLineHeight*i+30);  //+30是因为字体是从下向上画的
            }
        }

        //-以下内容可以方便地查看每个按钮的坐标是否正确;
        //testXY();
    }
    public Dimension getPreferredSize() {
        return (new Dimension(450,366));
    }

    public void dealComboxMouseDragged(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        if (x>slideButtonX && x<slideButtonX+50  && comboxVisibleNameList.size()>2) {
            slideButtonY=y;
            if (slideButtonY>slideButtonMaxY)
                slideButtonY=slideButtonMaxY;
            if (slideButtonY<slideButtonMinY)
                slideButtonY=slideButtonMinY;
            comboxTextStartY=(int) (comboxTextStartMaxY-(comboxTextStartMaxY -comboxTextStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));
            repaint();
        }
    }

    public void dealComboxMousePressed(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        //System.out.println("clicked x="+x+"   x0="+slideBgStartX0);

        if (x<comboxTextDeleteButtonStartX) {
            for (int i=0; i<comboxVisibleNameList.size(); i++){

                if (y>comboxTextStartY+comboxTextLineHeight*i && y<comboxTextStartY+comboxTextLineHeight*(i+1)) {
                    comboxChoiceIndex=i;
                    String nameStr=(String)comboxVisibleNameList.get(i);
                    String str=(String)comboxVisibleBirthList.get(i);
                    if (str.length()>=10){
                        birthYearField.setText(str.substring(0,4));
                        birthMonthField.setText(str.substring(5,7));
                        birthDayField.setText(str.substring(8,10));
                    }
                    str=(String)comboxVisibleSexList.get(i);
                    if (str.equals("man")){
                        manButton.setSelected(true);
                        womanButton.setSelected(false);
                    }else{
                        manButton.setSelected(false);
                        womanButton.setSelected(true);
                    }
                    userNameField.setText(nameStr);
                    isComboxHide=true;
                }
            }
            //System.out.println("labelStartY:"+labelStartY);
            repaint();
            userNamePanel.repaint();
        }

        if (x>comboxTextDeleteButtonStartX && x<slideBgStartX0) {
            for (int i=0; i<comboxVisibleNameList.size(); i++){
                if (y>comboxTextStartY+comboxTextLineHeight*i && y<comboxTextStartY+comboxTextLineHeight*(i+1)) {
                    comboxChoiceIndex=i;
                    deletingUser=(String)comboxVisibleNameList.get(i);
                    userNameField.setText(deletingUser);
                    //System.out.println("\n======SSSS"+deletingUser);
                    createDeleteWindow();
                }
            }
        }

        if (comboxVisibleNameList.size()<=2)
            comboxTextStartY=comboxTextStartMaxY; 

        if (x>slideButtonX && x<slideButtonX+50 && comboxVisibleNameList.size()>2) {
            if (y>slideButtonMaxY){
                comboxTextStartY-=50;
                if (comboxTextStartY<comboxTextStartMinY)
                    comboxTextStartY=comboxTextStartMinY; 

                slideButtonY=(int) (slideButtonMaxY-(slideButtonMaxY-slideButtonMinY)*(comboxTextStartY-comboxTextStartMinY)/(comboxTextStartMaxY-comboxTextStartMinY));
                if (slideButtonY>slideButtonMaxY) 
                    slideButtonY=slideButtonMaxY;
                if (slideButtonY<slideButtonMinY) 
                    slideButtonY=slideButtonMinY;

            }
            if (y<slideButtonMinY){
                comboxTextStartY+=50;
                if (comboxTextStartY>comboxTextStartMaxY)
                    comboxTextStartY=comboxTextStartMaxY; 
                slideButtonY=(int) (slideButtonMaxY-(slideButtonMaxY-slideButtonMinY)*(comboxTextStartY-comboxTextStartMinY)/(comboxTextStartMaxY-comboxTextStartMinY));
                if (slideButtonY>slideButtonMaxY) 
                    slideButtonY=slideButtonMaxY;
                if (slideButtonY<slideButtonMinY) 
                    slideButtonY=slideButtonMinY;
            }
            if (y>=slideButtonMinY && y<=slideButtonMaxY ) {
                slideButtonY=y;
                comboxTextStartY=(int) (comboxTextStartMaxY-(comboxTextStartMaxY -comboxTextStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));
            }

            //System.out.println("clicked comboxTextStartY:"+comboxTextStartY);
            repaint();
        }
    }

    public void createDeleteWindow(){
        JButton deleteYesButton=new JButton();
        ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
        deleteYesButton.setBackground(Color.white);
        deleteYesButton.setBorderPainted(false);
        deleteYesButton.setFocusPainted(false);
        deleteYesButton.setIcon(backIcon);
        deleteYesButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    deleteUserYes();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });
        deleteYesButton.setBackground(Color.darkGray);
        deleteYesButton.setContentAreaFilled(false);

        JButton deleteCancelButton=new JButton();
        ImageIcon backIcon2 = new ImageIcon("resource-common/images/login/sssss07.png","");
        deleteCancelButton.setBackground(Color.white);
        deleteCancelButton.setBorderPainted(false);
        deleteCancelButton.setFocusPainted(false);
        deleteCancelButton.setIcon(backIcon2);
        deleteCancelButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    deleteUserCancel();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        deleteCancelButton.setContentAreaFilled(false);

        JPanel userInfoPanel = new JPanel() {
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D) g ;
                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
                    g2.setColor(Color.white);
                    g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,22));
                    g2.drawString("您确认要删除用户: "+deletingUser+" 吗？",10,30);
                }
                public Dimension getPreferredSize() {
                    return (new Dimension(400,55));
                }
            };
        userInfoPanel.setBackground(Color.black);


        JPanel p = new DeletePanel() ;
        p.setLayout(null);
        p.add(userInfoPanel);
        userInfoPanel.setBounds(20,90,410,55);

        p.add(deleteYesButton);
        deleteYesButton.setBounds(70,165,121,43);
        p.add(deleteCancelButton);
        deleteCancelButton.setBounds(270,165,121,43);

        deleteWindow=new JDialog(ElfiiCommonRunInfo.getRunInfo().getMainFrame(),true);

		deleteWindow.setUndecorated(true);		  
        deleteWindow.setBackground(null);
        deleteWindow.setShape(new RoundRectangle2D.Float(0,0,dialogWidth,dialogHeight,30,30));  

        deleteWindow.setBounds(282,275,459,217);
        deleteWindow.getContentPane().setLayout(new BorderLayout());

        deleteWindow.getContentPane().add(p,BorderLayout.CENTER);

        deleteWindow.validate();
        deleteWindow.setVisible(true);
    }

    public void deleteUserYes(){
        String str="";
        ResultSet rs;
        String sex="",birth="",machineId="";
        try {
            Statement st = ElfiiCommonRunInfo.getRunInfo().getStatement1();

            str="select trainnerId,machineId,sex,birth from TrainningUser where trainnerId='"+deletingUser+"'" ;;
            rs=st.executeQuery(str);
            if (rs.next()){
                sex=rs.getString("sex");
                birth=rs.getString("birth");
                machineId=rs.getString("machineId");
            }

            str="insert into TrainningDeletedUser (trainnerId,machineId,sex,birth) values ('"+deletingUser+"','"+machineId+"','"+sex+"','"+birth+"')";
            st.execute(str);

            str="delete from TrainningUser where trainnerId='"+deletingUser+"'" ;
            st.execute(str);

            str="delete  from TrainningItemAPO where trainnerID='"+deletingUser+"' " ;
            st.execute(str);

            str="delete from trainningitemfpo where id_TrainnerInfoPO ='"+deletingUser+"' " ;
            st.execute(str);

            str="delete from trainningitemhpo where id_TrainnerInfoPO ='"+deletingUser+"' " ;
            st.execute(str);

            str="delete from trainingresultkpo where id_TrainnerInfoPO ='"+deletingUser+"'" ;
            st.execute(str);

            str="delete from trainningitemlpo where id_TrainnerInfoPO ='"+deletingUser+"' " ;
            st.execute(str);

            str="delete from trainningitemmpo where id_TrainnerInfoPO ='"+deletingUser+"' " ;
            st.execute(str);

            str="delete from trainningitemnpo where id_TrainnerInfoPO ='"+deletingUser+"' " ;
            st.execute(str);



        } catch (Exception ee) {System.out.print("login()  "+ee+"   "+str);}

        initDataList();
        userNameField.setText("");
        setFiltString("");
        repaint();

        deleteWindow.setVisible(false);
        deleteWindow.dispose();
    }

    public void deleteUserCancel(){
        deleteWindow.setVisible(false);
        deleteWindow.dispose();
    }

    class DeletePanel extends JPanel {

        BufferedImage deleteBgImage;
        BufferedImage bgImg;

        public DeletePanel(){
            File f;
            try {
                f=new File("resource-common/images/login/sssss03.png");
                deleteBgImage=ImageIO.read(f);

                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(dialogX0,dialogY0,dialogWidth,dialogHeight));

            } catch(Exception e) {System.out.print(e);}
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,42));
            g2.setColor(Color.blue);

            g2.drawImage(bgImg,null,0,0);
            g2.drawImage(deleteBgImage,null,0,0);

        }
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }

    public void initDataList(){
        comboxAllNameList  = new java.util.ArrayList();
        comboxAllSexList   = new java.util.ArrayList();
        comboxAllBirthList = new java.util.ArrayList();
        try {
            Statement st = ElfiiCommonRunInfo.getRunInfo().getStatement1();

            String str="select trainnerId,sex,birth,lastLoginDate,lastLoginTime from TrainningUser  order by lastLoginDate desc,lastLoginTime desc" ;
            ResultSet rs=st.executeQuery(str);
            while (rs.next()){
                str=rs.getString("trainnerId");
                comboxAllNameList.add(str);
                str=rs.getString("sex");
                comboxAllSexList.add(str);
                str=rs.getString("birth");
                comboxAllBirthList.add(str);
            }
        } catch(Exception e) {System.out.print(e+"   ");}
    }


}
